//
//  DateExt.swift
//  kano
//
//  Created by Sophia on 2024/8/14.
//

import Foundation


extension Date {
    
    func ka_timeInterval() -> TimeInterval {
        // 获取自1970年以来的时间戳，单位为秒
        let timeInterval = self.timeIntervalSince1970
        // 将秒数转换为毫秒
        let timestampInMilliseconds = timeInterval * 1000
        return TimeInterval(Int(timestampInMilliseconds))
    }
    
    // Date -> string
    func ka_stringWith(format: String) -> String {
        var var_formatter = DateFormatter()
        var_formatter.calendar = Calendar(identifier: .gregorian)
        var_formatter.dateFormat = format
        return var_formatter.string(from: self)
    }
    
    // calculate date
    func ka_dateByAdding(days: Int) -> Date {
        var var_dateComponents = DateComponents()
        var_dateComponents.day = days
        var var_date = Calendar(identifier: .gregorian).date(byAdding: var_dateComponents, to: self)
        return var_date!
    }
    
    func ka_dateBySubtracting(days: Int) -> Date {
        ka_dateByAdding(days: days * -1)
    }

}


extension Date {
    
    static func timeInterval(start: Date, end: Date) -> TimeInterval {
        let time = end.timeIntervalSince(start)
        return time
    }
    
    static func timeIntervalWith(startInterval: TimeInterval, endInterval: TimeInterval) -> TimeInterval {
        let date1 = Date(timeIntervalSince1970: startInterval)
        let date2 = Date(timeIntervalSince1970: endInterval)
        let difference = date2.timeIntervalSince(date1)
        //print("timeInterval differ: \(difference) seconds")
        return difference
    }
    
    //Date off by a few days
    static func ka_compare(start: Date, end: Date) -> Int {
        let var_calendar = Calendar(identifier: .gregorian)
        let var_components = var_calendar.dateComponents([.day], from: start, to: end)
        let var_differenceInDays = var_components.day!
        print("Date off by a few days \(var_differenceInDays) days")
        return var_differenceInDays
    }
    
    //Date——>String
    static func ka_string(with date: Date) -> String {
        let var_format = ka_getFormatter()
        let var_dateStr = var_format.string(from: date)
        return var_dateStr
    }
    static func ka_string(with date: Date, format: String) -> String {
        let var_format = DateFormatter()
        var_format.calendar = Calendar(identifier: .gregorian)
//        var_format.calendar = Calendar.current
        var_format.dateStyle = .medium
        var_format.timeStyle = .long
        var_format.timeZone = TimeZone.current
        var_format.dateFormat = format
        let var_dateStr = var_format.string(from: date)
        return var_dateStr
    }
    
    //String——>Date
    static func ka_date(from string: String) -> Date {
        let var_format = ka_getFormatter()
        let var_date = var_format.date(from: string)
        return var_date!
    }
    static func ka_date(from string: String, format: String) -> Date {
        //let var_format = ka_getFormatter()
        let var_format = DateFormatter()
        var_format.calendar = Calendar(identifier: .gregorian)
        var_format.dateStyle = .medium
        var_format.timeStyle = .long
        var_format.timeZone = TimeZone.current
        var_format.dateFormat = format
        let var_date = var_format.date(from: string)
        return var_date ?? Date()
    }
    static func ka_date(from string: String, locale: String) -> Date {
        let var_format = ka_getFormatter()
        let var_date = var_format.date(from: string)
        return var_date!
    }
    
    static func ka_getFormatter() -> DateFormatter {
        let var_formatter = DateFormatter()
        var_formatter.calendar = Calendar(identifier: .gregorian)
        var_formatter.dateStyle = .medium
        var_formatter.timeStyle = .long
        var_formatter.dateFormat = "YYYY/MM/dd"
        var_formatter.timeZone = TimeZone.current
        return var_formatter
    }
    //TimeInterval -> String
    static func ka_stringWith(timestamp: TimeInterval, format: String) -> String {
        let date = Date(timeIntervalSince1970: timestamp)
        let var_format = DateFormatter()
        var_format.calendar = Calendar(identifier: .gregorian)
        var_format.dateStyle = .medium
        var_format.timeStyle = .long
        var_format.timeZone = TimeZone.current
        var_format.dateFormat = format
        let var_dateStr = var_format.string(from: date)
        return var_dateStr
    }
    
    static func ka_dateWithDaysBeforeNow(days: Int) -> Date {
        Date().ka_dateBySubtracting(days: days)
    }
    
    static func ka_currentData() -> String {
//        let currentDate = Date()
//        
//        let formatter = DateFormatter()
//        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
//        formatter.timeZone = TimeZone.current
//        
//        let currentTimeString = formatter.string(from: currentDate)
//        return currentTimeString

        return "\(Int(Date().timeIntervalSince1970 * 1000))"
    }
}
